/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jdbc.dao.impl;

import com.jdbc.daos.UserDao;
import com.jdbc.domains.User;
import com.jdbc.driver.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 虎
 */
public class UserDaoImpl implements UserDao{
    Connection conn = null;
    ResultSet res =null;
    PreparedStatement st = null;
    @Override
    public void save(User user) {
        try{
            
            conn = JdbcUtil.getConn();
            /*
            st = conn.createStatement();
            String sql = "insert into tables values ('"+user.getUserID()+"','"+user.getPassword()+"','"+user.getName()+"','"+user.getSex()+"','"+user.getID()+"','"+user.getProvince()+"','"+user.getPhone()+"','"+user.getEmail()+"')";
            st.executeUpdate(sql);
            */
            String sql = "insert into tables values (?,?,?,?,?,?,?,?)";
            st = conn.prepareStatement(sql);
            st.setString(1,user.getUserID());
            st.setString(2,user.getPassword());
            st.setString(3,user.getName());
            st.setString(4,user.getSex());
            st.setString(5,user.getID());
            st.setString(6,user.getProvince());
            st.setString(7,user.getPhone());
            st.setString(8,user.getEmail());
            st.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(conn, st, null);
        }
    }

    @Override
    public void delete(String username) {
        conn = JdbcUtil.getConn();
        String sql = "delete from tables where username = ?";
        try {
            st = conn.prepareStatement(sql);
            st.setString(1,username);
            st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.close(conn, st, null);
        }
    }

    @Override
    public void update(User user) {
       
    }

    @Override
    public User get(String username) {
        conn = JdbcUtil.getConn();
        String sql = "select * from tables where username = ?";
        User user = new User();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1,username);
            res = st.executeQuery(sql);
            user.setUserID(res.getString("username"));
            user.setPassword(res.getString("password"));
            user.setID(res.getString("id"));
            user.setSex(res.getString("sex"));
            user.setName(res.getString("name"));
            user.setEmail(res.getString("email"));
            user.setPhone(res.getString("phone"));
            user.setProvince(res.getString("province"));
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.close(conn, st, null);
        }
        return null;
    }

    @Override
    public List<User> listAll() {
        try{
            conn = JdbcUtil.getConn();
            String sql = "select * from tables";
            st = conn.prepareStatement(sql);
            res = st.executeQuery(sql);
            List<User> userList = new ArrayList<User>();
            while(res.next()){
                User u = new User();
                u.setUserID(res.getString("username"));
                u.setPassword(res.getString("password"));
                userList.add(u);
            }
            return userList;
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(conn, st, null);
        }
        return null;
    }
}
